package com.hrbu.ssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrbu.ssm.bean.Dept;
import com.hrbu.ssm.bean.Employee;
import com.hrbu.ssm.mapper.EmployeeMapper;
import com.hrbu.ssm.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Author: tanyajun
 * @CreateTime: 2021/3/25
 * @Blog: https://tanyajun.top
 */
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getEmployeeList() {
        return employeeMapper.selectByExample(null);
    }

    @Override
    public List<Employee> getEmpListByPage(Integer page, Integer limit) {
        Page<Employee> deptPage = PageHelper.startPage(page, limit);
        List<Employee> empList = employeeMapper.selectAllEmpAndDept();
        return empList;
    }

    @Override
    public Long getTotal() {
        return employeeMapper.countByExample(null);
    }

    @Override
    public Integer update(Employee employee) {
        if (employee == null || employee.getId() == null || employee.getId() < 0) {
            return 0;
        }
        try {
            return employeeMapper.updateByPrimaryKeySelective(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Employee findEmpById(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer delete(Employee employee) {
        if (employee == null || employee.getId() == null || employee.getId() < 0) {
            return 0;
        }
        return employeeMapper.deleteByPrimaryKey(employee.getId());
    }

    @Override
    public List<Employee> queryByMind(String empName, String startDate, String endDate, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return employeeMapper.queryByMind(empName, startDate, endDate);
    }

    @Override
    public Integer queryByMindTotalNum(String empName, String startDate, String endDate) {
        List<Employee> employeeList = employeeMapper.queryByMind(empName, startDate, endDate);
        return employeeList == null ? 0 : employeeList.size();
    }

    @Override
    public Integer insert(Employee employee) {
        return employeeMapper.insertAndBindPramaryKey(employee);
    }
}
